package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImage;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.ProdPageVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
* @author 31803
* @description 针对表【tb_prod】的数据库操作Service实现
* @createDate 2025-05-27 09:35:32
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    FastFileStorageClient fileStorageClient;
    @Autowired
    TbProdImageMapper tbProdImageMapper;

    @Override
    public Result ProdList(ProdPageVo prodPageVo) {
        Page<TbProd> page = new Page<>(prodPageVo.getPageNum(), prodPageVo.getPageSize());
//        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
        Page<TbProd> selectProdList = tbProdMapper.selectProdList(page, prodPageVo);
        for (TbProd record : selectProdList.getRecords()) {
            List<TbProdImage> tbProdImages = tbProdImageMapper.selectList(new QueryWrapper<TbProdImage>().lambda().eq(TbProdImage::getProdId, record.getProdId()));
            record.setTbProdImageList(tbProdImages);
        }
        return Result.ok().setData(selectProdList);
    }

    @Override
    public Result AddProd(TbProd tbProd) {
        TbProd prod = tbProdMapper.selectOne(new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu()));
        if(prod!=null){
          return Result.error().setMsg("Spu名称重复");
        }
        tbProd.setProdStastus(0);
        tbProdMapper.insert(tbProd);

       if(tbProd.getTbProdImageList()!=null||tbProd.getTbProdImageList().size()!=0){
           for (TbProdImage tbProdImage : tbProd.getTbProdImageList()) {
               tbProdImage.setProdId(tbProd.getProdId());
               tbProdImageMapper.insert(tbProdImage);
           }
       }
        return Result.ok();
    }

    @Override
    public Result UpProdImage(MultipartFile file) {
        String url= null;
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            url = "http://192.168.80.192:8888/"+storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error().setCode(500).setMsg("上传失败");
            //throw new RuntimeException(e);
        }

    }

    @Override
    public Result DeleteAll(List<TbProd> tbProdList) {
        if(tbProdList==null||tbProdList.size()==0){
            return Result.error().setCode(500).setMsg("请选择需要删除的数据");
        }
        tbProdList.stream().forEach(item->{
            tbProdMapper.deleteById(item.getProdId());
        });
        return Result.ok();
    }

    @Override
    public Result UpdataProd(TbProd tbProd) {
        //查询修改商品是否存在
        TbProd prod = tbProdMapper.selectById(tbProd.getProdId());
        if(prod==null){
           return Result.error().setCode(500).setMsg("您修改的商品不存在");
        }
        //修改商品
        tbProdMapper.updateById(tbProd);
        //删除以前图片
        int delete = tbProdImageMapper.delete(
                new QueryWrapper<TbProdImage>().lambda().eq(TbProdImage::getProdId, tbProd.getProdId())
        );
        //加载新的图片
       if(tbProd.getTbProdImageList()!=null&&tbProd.getTbProdImageList().size()>0) {
           for (TbProdImage tbProdImage : tbProd.getTbProdImageList()) {
               tbProdImage.setProdId(tbProd.getProdId());
               tbProdImageMapper.insert(tbProdImage);
           }
       }

        return Result.ok();
    }
}




